/*
 * TextWindow.fx
 *
 * Created on Apr 12, 2010, 11:54:26 AM
 */

package fxgreader;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.ext.swing.SwingComponent;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import java.awt.Dimension;
import javafx.scene.shape.Rectangle;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import fxgreader.components.CloseIcon;
import javafx.scene.layout.HBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.image.ImageView;
import javafx.scene.Cursor;
import javax.swing.*;
import javax.swing.*;
import java.lang.Exception;
import fxgreader.ThreadUtil;
/**
 * @auimport fxgreader.components.CloseIcon;
thor perkintang
 */

public class TextWindow extends CustomNode{
public var translateY_to : Number;
public var width : Number = 800 on replace{
    ajustSize();
};
public var height : Number = 600;
public var onClose : function();
var startX = 0.0;
var startY = 0.0;
public var content : String = "hello" on replace{
    text.setText(content);
    text.setCaretPosition(0);
};
var text: JTextPane;
var pane: JScrollPane;
public function close(){
        onClose();
        delete articlenode from group.content;
        delete textnode from group.content;
        articlenode = null;
        ThreadUtil.stopRunningTask();
}
var closeButton : CloseIcon = CloseIcon{
                                    blocksMouse : true;
                                    onClick: function(){
                                        close();
                                    }
                                    translateX : bind width-10-20
                                    translateY : 10
                                };
var starButton : IconButton = IconButton{y : IconButton.STAR};
var likeButton : IconButton = IconButton{y : IconButton.LIKE};
var shareButton : IconButton = IconButton{y : IconButton.SHARE};
var forwardButton : ImageView = ImageView {
                        image:  Resources.forwardImage
                        cache : true
                        cursor: Cursor.HAND
                        onMouseClicked : function(event:MouseEvent):Void{
//                            Main.api.getFullText(Main.currentArticle.getLink(), function(is: java.io.InputStream):Void{
//
//                                var map : Map = HttpUtil.parseHtml(content);
//                                Main.currentArticle.setBody(map.get("content") as String);
//                                Main.body.content = Main.currentArticle.getBody();
//                            });
                              text.setText("loading ...");
                              delete forwardButton from hb.content;
                              insert backButton into hb.content;
                              ThreadUtil.runSeparately(function(){
                                  try{
                                      var panel = HtmlUtil.getPanel(Main.currentArticle.getLink());
                                      FX.deferAction(function(){
                                      panel.setPreferredSize(new Dimension(width-50, height-100));
                                      articlenode = SwingComponent.wrap(panel);
                                      articlenode.translateX = 25;
                                      articlenode.translateY = 50;
                                      articlenode.blocksMouse = true;
                                      delete textnode from group.content;
                                      insert articlenode into group.content;
                                      FX.deferAction(function(){
                                          HtmlUtil.loadPage()
                                      });
                                      });
                                  }catch(e : Exception){
                                      FX.deferAction(function(){
                                          delete articlenode from group.content;
                                          delete textnode from group.content;
                                          insert textnode into group.content;
                                          text.setText("can't open this page, some error occurred!");
                                      });
                                  }
                              });
                        }
                    };
var backButton : ImageView = ImageView {
                        image:  Resources.backImage
                        cache : true
                        cursor: Cursor.HAND
                        onMouseClicked : function(event:MouseEvent):Void{
                              text.setText(content);
                              delete articlenode from group.content;
                              delete textnode from group.content;
                              insert textnode into group.content;
                              ThreadUtil.stopRunningTask();
                              delete backButton from hb.content;
                              insert forwardButton into hb.content;
                        }
                    };
var hb : HBox = HBox{ spacing: 10
                  translateY : bind height - 32 - 5
                  translateX : bind (width+15)/2 - (32*3+10*2)/2
                  content:[starButton,likeButton,shareButton,forwardButton]
             };
override var onMousePressed = function(e:MouseEvent):Void {
    startX = e.sceneX-translateX;
    startY = e.sceneY-translateY;
}

override var onMouseDragged = function(e:MouseEvent):Void {
    var tx = e.sceneX-startX;
    if(tx < 0) { tx = 0; }
    translateX = tx;
    var ty = e.sceneY-startY;
    if(ty < 0) { ty = 0; }
    translateY = ty;
}
public function show(){
    Main.api.onMarkSuccess = null;
    Main.api.markArticle(Main.currentArticle, Main.api.READ, Main.api.ADD,true);
    visible = true;
    ajustSize();
    toFront();

}
function ajustSize(){
    delete textnode from group.content;
    var article = Main.currentArticle;
    content = article.getBody();
    starButton.marked = article.isStarred();
    shareButton.marked = article.isShared();
    likeButton.marked = article.isLiked();
    text  = new JTextPane();
    pane = new JScrollPane(text,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    text.setText(content);
    text.setPreferredSize(new Dimension(width-50, height-100));
    textnode = SwingComponent.wrap(pane);
    textnode.translateX = width/2 - textnode.boundsInLocal.width/2;
    textnode.translateY = 50;
    textnode.blocksMouse = true;
    insert textnode into group.content;
    delete backButton from hb.content;
    delete forwardButton from hb.content;
    insert forwardButton into hb.content;
}
var textnode : Node;
var articlenode : Node;
var group : Group;
override protected function create () : Node {
    var background = Rectangle {
                    blocksMouse : true;
                    stroke : Color.WHITE
                    width:  bind width  height: bind height
                    arcWidth: 20  arcHeight: 20
                    opacity:0.3
             };
    return group = Group{
                content :[background,closeButton,hb]
            }
}
}
